package com.businessportal.web.dataaccess.dao;

import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.entities.Uploads;
import com.businessportal.web.dataaccess.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by Vadim on 7/20/14.
 */
public class UploadsDao implements IUploadsDao {

    @Override
    public Uploads getById(Long aLong) {
        Session session = null;
        Uploads retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            retValue = (Uploads)session.get(Uploads.class, aLong);

            session.getTransaction().commit();
        } catch (Exception e){

            retValue = null;
            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public List<Uploads> getAll() {
        Session session = null;
        List<Uploads> retValue = null;

        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Query userQuery = session.createQuery("From Uploads where 1=1");

            retValue = userQuery.list();

            session.getTransaction().commit();

        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public void save(Uploads uploads) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.save(uploads);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void update(Uploads uploads) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.saveOrUpdate(uploads);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void delete(Uploads uploads) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(uploads);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void deleteById(Long aLong) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(new Login(aLong));

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public Set<Uploads> getUploadsForUser(Login user) {
        Session session = null;
        Set<Uploads> retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            String sql = String.format("from Uploads where login_id = %s", user.getId());

            Query blogQuery = session.createQuery(sql);

            List<Uploads> retList = blogQuery.list();

            retValue = new TreeSet<Uploads>(retList);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }
}
